package com.wenhao.interview.huawei;

import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
import java.util.TreeSet;

/**
 * @author Wenhao Tong
 * @create 2021-07-21 19:45
 */
public class Main2 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();
        int K = scanner.nextInt();
        int[][] equipment = new int[K][2];
        for (int i = 0;i < K;i++) {
            equipment[i][0] = scanner.nextInt();
            equipment[i][1] = scanner.nextInt();
        }
        Arrays.sort(equipment, new Comparator<int[]>() {
            @Override
            public int compare(int[] t1, int[] t2) {
                if (t1[1] == t2[1]) {
                    return t1[0] - t2[0];
                }
                return t1[1] - t2[1];
            }
        });
        TreeSet<Integer> heap = new TreeSet<>();
        for (int i = 0;i < equipment.length;i++) {
            if (heap.size() == N) {
                int temp = heap.pollFirst() + equipment[i][0];
                heap.add(temp);
            } else {
                heap.add(equipment[i][0]);
            }
        }
        int min = 0;
        while (heap.size() > 0) {
            int temp = heap.pollFirst();
            min = min > temp ? min : temp;
        }
        System.out.println(min);
    }
}
